package com.sntk.organauthomanager.application.service;

import com.sntk.organauthomanager.application.dto.OrganizationCreator;
import com.sntk.organauthomanager.application.dto.OrganizationDTO;
import com.sntk.organauthomanager.domain.model.Organization;
import com.sntk.organauthomanager.domain.repository.OrganizationRepository;
import com.sntk.organauthomanager.infrastructure.commit.HisOrganCommit;
import com.sntk.organauthomanager.infrastructure.exception.ObjectNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * @ClassName: OrganizationApplicationService
 * @author: wgchao
 * @createTime: 2020/7/20 4:51 PM
 * @Description: TODO
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OrganizationApplicationService {
    @Autowired
    private OrganizationRepository organizationRepository;

    private HisOrganCommit hisOrganCommit;

    /**
     * 创建组织机构
     * @param creator
     * @param operator
     */
    public void createOrganization(OrganizationCreator creator, String operator){
        Organization superiorOgi = null;
        if(!StringUtils.isEmpty(creator.getSuperiorOgi())){
            superiorOgi = organizationRepository.findByKey(creator.getSuperiorOgi());
            if(superiorOgi == null){
                throw new ObjectNotFoundException("上级机构不存在");
            }
        }
        String institutionNumber = organizationRepository.generateNumber(creator.getSuperiorOgi());

        //TODO 如果his机构编号不存在 在his系统中创建
//        if(StringUtils.isEmpty(creator.getHisInstitutionNumber())){
//            creator.setHisInstitutionNumber(hisOrganCommit.createHisOrgan(creator.getInstitutionName()));
//        }

        Organization organization = Organization.generate(institutionNumber, creator.getInstitutionName(),
                creator.getInstitutionalAddress(), creator.getHisInstitutionNumber(), creator.getRemark(), superiorOgi);
        organizationRepository.add(organization, operator);
    }

    /**
     * 修改机构信息
     * @param organizationDTO
     * @param operator
     */
    public void modifyOrganInfo(OrganizationDTO organizationDTO, String operator){
        Organization organization = organizationRepository.findByKey(organizationDTO.getInstitutionNumber());
        if(organization == null){
            throw new ObjectNotFoundException("机构不存在");
        }
        organization.modifyOrganInfo(organizationDTO.getInstitutionName(), organizationDTO.getInstitutionalAddress(),
                organizationDTO.getHisInstitutionNumber(), organizationDTO.getRemark(), organizationDTO.isDisabled());
        organizationRepository.add(organization, operator);
    }

    /**
     * 删除机构
     * @param institutionNumber
     * @param operator
     */
    public void removeOrgan(String institutionNumber, String operator){
        Organization organization = organizationRepository.findByKey(institutionNumber);
        if(organization == null){
            throw new ObjectNotFoundException("机构不存在");
        }
        organizationRepository.delete(institutionNumber, operator);
    }

}
